import { utils } from "elmer-common";
import { createProvider } from "../Components/Provider";
import { createMicroAppProvider, useMicroApp } from "../Components/MicroApp";

interface IConfigStore {
    pages: any;
}

interface ICreatePageOption {
    urlPrefix?: string;
}

interface ICreatePagesOption extends ICreatePageOption {
    useOtherPages?: string[]
}

const configStore: IConfigStore = {
    pages: {}
};
const createPage = (namespace: string, data: AppView.IPage, options?: ICreatePageOption) => {
    if(!configStore.pages[namespace]) {
        configStore.pages[namespace] = [];
        configStore.pages[namespace].urlPrefix = options?.urlPrefix || "/";
    }
    let hasSamePage = false;
    for(const item of configStore.pages[namespace]) {
        if(item.id === data.id) {
            hasSamePage = true;
            break;
        }
    }
    (data as any).routerPathName = data.pathname;
    if(!hasSamePage) {
        if(options && !utils.isEmpty(options?.urlPrefix)) {
            data.pathname = (options.urlPrefix + data.pathname).replace(/\/\//g, '/');
        }
        configStore.pages[namespace].push(data);
    }
    const createNextPage = (nextPage: AppView.IPage) => {
        createPage(namespace, nextPage);
        return createNextPage;
    };
    return {
        createPage: createNextPage
    }
};
/**
 * 注册页面信息
 * @param namespace 声明空间
 * @param data 页面数据
 * @param useOtherPages 其他参数
 * @returns 
 */
export const createPages = (namespace: string, data: AppView.IPage[], options?: ICreatePagesOption) => {
    const { useOtherPages, ...createPageOptions } = options ?? {};
    const includeNamespaces: string[] = [...(useOtherPages || [])];
    if(!configStore.pages[namespace]) {
        configStore.pages[namespace] = [];
        configStore.pages[namespace].urlPrefix = options?.urlPrefix || "/";
    }
    if(utils.isEmpty(namespace)) {
        throw new Error("创建页面信息失败，namespace不能为空。");
    }
    data.forEach((item) => {
        createPage(namespace, item, createPageOptions);
    });
    !includeNamespaces.includes(namespace) && includeNamespaces.push(namespace);
    const out = (): AppView.IPagesData => {
        const outData: any = {};
        includeNamespaces.forEach((names) => {
            outData[names] = configStore.pages[names];
        });
        return outData;
    };
    const createNextPage = (page: AppView.IPage) => {
        createPage(namespace, page, createPageOptions);
        return {
            createPage: (nextPage: AppView.IPage) => createNextPage(nextPage),
            createNextPages: (nextNamespace: string, nextPages: AppView.IPage[]) => {
                return createPages(nextNamespace, nextPages, {
                    ...(options || {}),
                    useOtherPages: includeNamespaces
                });
            }
        };
    };
    return {
        createPage: createNextPage,
        createNextPages: (nextNamespace: string, nextPages: AppView.IPage[], nextOptions?: ICreatePagesOption) => createPages(nextNamespace, nextPages, {
            ...(nextOptions||{}),
            useOtherPages: includeNamespaces
        }),
        out
    };
};

export const createApp = (option: AppView.IMicroAppOption) => {
    const Provider = createMicroAppProvider(option);
    return {
        Provider,
        useMicroApp
    }
};

export const createRootApp = (options: AppView.IRootAppOptions) => {
    return createProvider(options);
};
